<?php
// 本类由系统自动生成，仅供测试用途
class BrrowAction extends CommonAction {
	 public function _filter(&$map){
		$userid=$_SESSION[C('USER_AUTH_KEY')];
		$User=M('User');
		//得到该用户所在部门
		$department=$User->where('id='.$userid)->getField('depid');
		$bs=$this->isapprove($userid); 
		 if(!$bs&&$userid!=1){
			$map['username'] = $_SESSION['username'];
		}elseif($bs){
			//如果具有审批权限，则能看到该部门的借款信息
			$map['depid']=$department;
		}else{
			$map[]='';
		} 		
	} 
	public function _before_index() {
		$userid=$_SESSION[C('USER_AUTH_KEY')];
		//$role=D('Role');
		//管理员的id默认为1,获得是否具有审批权限
		$bs=$this->isapprove($userid);
		//有审批权限或者是管理员
		if($bs||$userid==1){
			$this->assign('app','isapprove');
		}
		$re=$this->isRepay($userid);
		if($re||$userid==1){
			$this->assign('repay','isrepay');
		}
		
	}
	public function _before_add() {
		$userid=$_SESSION[C('USER_AUTH_KEY')];
		$username=$_SESSION['username'];
		$model = M ( "Brrow" );
		$max = $model->where("username='".$username."'")->min ('adduplim');
		if($max!=null){
			$this->assign ( 'max', $max );
		}else{
			$this->assign('max',$_SESSION['borrowamount']);
		}
		$user=M('User');
		$depid=$user->where('id='.$userid)->getField('depid');
		$this->assign('depid',$depid);
	}
	public function _before_edit() {
		$model = M ( "Brrow" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
	}
	public function _before_insert(){
		$name=$this->getActionName();
		$model = D ($name);
		$limit=$_SESSION['borrowamount'];
		if($limit==0){
			$this->ajaxReturn(1,'您的可借款金额为0！',300);
		}
		$allCount=0;
		$username=$_REQUEST['username'];//得到借款人
		$records=$model->where("username='".$username."'"." and bstat!=100")->field('bcount')->select();
		if($records!==false){
			for($i=0;$i<count($records);$i++){
				$count=$records[$i];
				$allCount+=$count['bcount'];
			}
		}
		if($allCount==0){
			//当数据库中还没有此人的借款信息时
			$_POST['bmcount']=$_REQUEST['bcount'];//得到借款累计
			$_POST['adduplim']=$limit-$_REQUEST['bcount'];//得到累计额度
		}else{
			//当数据库中有多条此人的借款信息
			$_POST['bmcount']=$allCount+$_REQUEST['bcount'];
			$_POST['adduplim']=$limit-$_REQUEST['bcount']-$allCount;
		}
		
		//更新已还款的累计金额
		$res=$model->where("username='".$username."'"." and bstat=100")->select();
		if(!empty($res))
		{
			for ($i=0;$i<count($res);$i++)
			{
				$money=$res[$i];
				$maps['bmcount']=$_POST['bmcount'];
				$maps['adduplim']=$_POST['adduplim'];
				$model->where('id='.$money['id'])->save($maps);
			}
		}
		
	}
	public function approve(){
		$id=$_REQUEST['id'];
		$model = M('Brrow');
		$approver=$_SESSION['username'];
		$state=$model->where('id='.$id)->getField('appstat');
		if($state==1){
			$this->ajaxReturn(1,'不能审批已审批过的记录！',300);
		}
		$result=$model->where('id='.$id)->setField('appstat','1');
		$model->where('id='.$id)->setField('userid',$approver);
		if($result!==false){
			$this->ajaxReturn (1,'审批成功!',200);
		}else{
			$this->ajaxReturn (1,'审批失败!',300);
		}
	}
	
	public function pay()
	{
		$id=$_REQUEST['id'];
		$model=M('Brrow');
		$list=$model->where('id='.$id)->find();
	   $backcount= $list['backcount'];
	   $bcount=$list['bcount'];
	    $max=$bcount-$backcount;
	    $this->assign('max',$max);
		$this->assign('list',$list);
		$this->display();
	}
	public function repay(){
		$name=$this->getActionName();
		$model = D ( $name );
		$id=$_REQUEST['id'];
		$capacity=$_REQUEST['capacity'];
		$list=$model->where('id='.$id)->find();
		//得到审批状态
		$appstat=$list['appstat'];
		if($appstat==0){
			$this->ajaxReturn(1,'不能操作未审批的记录',300);
		}
		//得到还款状态
		$bstat=$list['bstat'];
		if($bstat==100){
			$this->ajaxReturn(1,'不能操作已还款的记录',300);
		}
		$bcount=$list['bcount'];//借款数目
		$backcout=$list['backcount'];//获得已还的金额
		$res=$bcount-($capacity+$backcout);
		if($res<0)
		{
			$this->ajaxReturn(1,'还款金额大于借款金额',300);
		}
		//$backcout=$list['backcount'];
		$map['backcount']=$backcout+$capacity;
		$map['bstat']=$map['backcount']/$bcount*100;
		
		//如果这次还清了，修改借款累计和累计额度
		if($map['bstat']==100)
		{
			$repayer=$list['username'];//还款人
			$map['bmcount']=$this->getCount($repayer,$id);
			$map['adduplim']=$_SESSION['borrowamount']-$map['bmcount'];
			$res=$model->where('username=\''.$repayer.'\' and bstat=100')->select();
			if(!empty($res))
			{
				for($i=0;$i<count($res);$i++){
					$money=$res[$i];
					$mmap['bmcount']=$map['bmcount'];
					$mmap['adduplim']=$map['adduplim'];
					$model->where('id='.$money['id'])->save($mmap);
			    }
			}
		
			$lists=$model->where('id >'.$id.' and bstat!=100  and username=\''.$repayer.'\'')->order('id asc')->select();
			if(!empty($lists))
			{
				for ($j = 0; $j < count($lists); $j++) 
				{
					$moneys=$lists[$j];
					$maps['bmcount']=$this->getCounts2($repayer, $moneys['id'],$id);
					$maps['adduplim']=$_SESSION['borrowamount']-$maps['bmcount'];
					$model->where('id='.$moneys['id'])->save($maps);
				}
			}
			
		}
		$result=$model->where('id='.$id)->save($map);
		
		
	
		
		//设置还款状态
		//$result=$model->where('id='.$id)->setField('bstat',1);
		 $info=$this->getType($name);
		$pk=$model->getPk();
		$id1= $_REQUEST [$pk];
		if (false !== $result) {
				
			$this->insertLog('修改','操作'.$info.'编号为'.$id1.'的数据成功');
			//成功提示
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->ajaxReturn (1,'还款成功!',200);
		} else {
			//错误提示
			$this->insertLog('修改','操作'.$info.'编号为'.$id1.'的数据失败');
			$this->ajaxReturn (1,'还款失败!',300);
		} 
	}
	
	//获得未还款的借款累计
	public function getCount($username,$id)
	{
		$model=M('Brrow');
		$list=$model->where('username=\''.$username.'\'  and bstat!=100 and id!='.$id)->select();
		$totalcount=0;
		if(!empty($list))
		{
			for($i=0;$i<count($list);$i++){
				$money=$list[$i];
				$totalcount+=$money['bcount'];
			}
		}
		return $totalcount;
	}
	public function isapprove($id){
		$model=M('Access');
		$rs=$model->query('select * from mlt_access a ,mlt_role_user b where node_id=303 and pid=95 and level=3 and a.role_id=b.role_id and b.user_id='.$id);		
		if($rs){
			return true;
		}else{
			return false;
		}
	}
	public function isRepay($id){
		$model=M('Access');
		$rs=$model->query('select * from mlt_access a ,mlt_role_user b where node_id=304 and pid=95 and level=3 and a.role_id=b.role_id and b.user_id='.$id);
		if($rs){
			return true;
		}else{
			return false;
		}
	}
	
	
	public function _before_foreverdelete()
	{
		$username= $_SESSION['username'];
		$id = $_REQUEST ['ids'];
	    $model=M('Brrow');
		$list=$model->where('id not in ('.$id.')  and bstat!=100')->order('id asc')->select();
		
		if(!empty($list))
		{
			for ($i=0;$i<count($list);$i++)
			{
				$money=$list[$i];
				$map['bmcount']=$this->getCounts2($username, $money['id'],$id);
				$map['adduplim']=$_SESSION['borrowamount']-$map['bmcount'];
				$model->where('id='.$money['id'])->save($map);
			}
		}
		$list1=$model->where('id not in ('.$id.')  and bstat=100')->order('id asc')->select();
		if(!empty($list1))
		{
			for ($j=0;$j<count($list1);$j++)
			{
			$money1=$list1[$j];
			$map1['bmcount']=$this->getCounts1($username, $id);
			$map1['adduplim']=$_SESSION['borrowamount']-$map1['bmcount'];
			$model->where('id='.$money1['id'])->save($map1);
			}
		}
		
		//dump($list);
		//$this->ajaxReturn(1,'',200);
	}
	//获得未还款的且不在id范围中的借款累计
	public function getCounts1($username,$id)
	{
		$model=M('Brrow');
		$list=$model->where('username=\''.$username.'\'  and bstat!=100 and id not in('.$id.')')->select();
		$totalcount=0;
		if(!empty($list))
		{
			for($i=0;$i<count($list);$i++){
				$money=$list[$i];
				$totalcount+=$money['bcount'];
			}
		}
		return $totalcount;
	}
	
	//获得未还款的且id不在ids范围中且<id的借款累计
	public function getCounts2($username,$id,$ids)
	{
		$model=M('Brrow');
		$list=$model->where('username=\''.$username.'\'  and bstat!=100 and id <='.$id.' and id not in ('.$ids.')')->select();
		$totalcount=0;
		if(!empty($list))
		{
			for($i=0;$i<count($list);$i++){
				$money=$list[$i];
				$totalcount+=$money['bcount'];
			}
		}
		return $totalcount;
	}
	//获得未还款的id不是ids且<i的d借款累计
	public function getCounts($username,$id,$ids)
	{
		$model=M('Brrow');
		$list=$model->where('username=\''.$username.'\'  and bstat!=100 and id <='.$id.' and id!='.$ids)->select();
		$totalcount=0;
		if(!empty($list))
		{
			for($i=0;$i<count($list);$i++){
				$money=$list[$i];
				$totalcount+=$money['bcount'];
			}
		}
		return $totalcount;
	}
}